package com.msh.starter.common.web.config;

import com.msh.frame.client.interfaces.IClassLoadLog;
import com.msh.frame.web.aspect.ControllerInfoLogAspect;
import com.msh.frame.web.aspect.ServiceInfoLogAspect;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class AopLogConfig implements IClassLoadLog {
  private boolean isLoadControllerInfoLogAspect;
  private boolean isLoadServiceInfoLogAspect;
  @Bean
  @ConditionalOnProperty(name = "msh.starter.web.aop.log.controller.enable", havingValue = "true")
  public ControllerInfoLogAspect controllerInfoLogAop() {
    isLoadControllerInfoLogAspect = true;
    return new ControllerInfoLogAspect();
  }


  @Bean
  @ConditionalOnProperty(name = "msh.starter.web.aop.log.service.enable", havingValue = "true")
  public ServiceInfoLogAspect serviceInfoLogAspect() {
    isLoadServiceInfoLogAspect = true;
    return new ServiceInfoLogAspect();
  }

  @Override
  public String logInfo() {
    if(!isLoadControllerInfoLogAspect && !isLoadServiceInfoLogAspect){
      return null;
    }
    StringBuilder sb = new StringBuilder().append("----执行类 ")
        .append(AopLogConfig.class)
        .append("\r\n");
    if(isLoadControllerInfoLogAspect){
      sb.append("--------加载类 ")
          .append(ControllerInfoLogAspect.class)
          .append("\r\n");
    }
    if(isLoadServiceInfoLogAspect){
      sb.append("--------加载类 ")
          .append(ServiceInfoLogAspect.class)
          .append("\r\n");
    }
    return sb.toString();
  }
}
